package models

import (
	_ "time"

	orm "go-papers/database"
	"go-papers/tools"
)

type OrderCourse struct {
	OrderCourseId   int     `json:"orderCourseId" gorm:"type:int(11) unsigned;primary_key"` //
	OrderId         int     `json:"orderId" gorm:"type:int(11);"`                           // 订单id
	CourseId        int     `json:"courseId" gorm:"type:int(11);"`                          // 课程id
	MemberId        int     `json:"memberId" gorm:"type:int(11);"`                          // 购买的会员id
	CoursePrice     float64 `json:"coursePrice" gorm:"type:decimal(10,2);"`                 // 课程原价
	CoursePayPrice  float64 `json:"coursePayPrice" gorm:"type:decimal(10,2);"`              // 课程成交加价格
	CourseName      string  `json:"courseName" gorm:"type:varchar(255);"`                   // 课程名称
	CourseCode      string  `json:"courseCode" gorm:"type:varchar(255);"`                   // 课程编号
	CourseImg       string  `json:"courseImg" gorm:"type:varchar(255);"`                    // 课程封面图
	CourseType      string  `json:"courseType" gorm:"type:varchar(255);"`                   // 课程类目
	CourseTeach     string  `json:"courseTeach" gorm:"type:varchar(255);"`                  // 授课方式 ：线上 、 线下
	CourseCategory  string  `json:"courseCategory" gorm:"type:varchar(255);"`               // 学科科目
	CourseOrganName string  `json:"courseOrganName" gorm:"type:varchar(255);"`              // 课程机构名称
	CourseBrandName string  `json:"courseBrandName" gorm:"type:varchar(255);"`              // 课程品牌名称
	DataScope       string  `json:"dataScope" gorm:"-"`
	Params          string  `json:"params"  gorm:"-"`
	BaseModel
}

func (OrderCourse) TableName() string {
	return "order_course"
}

// 创建OrderCourse
func (e *OrderCourse) Create() (OrderCourse, error) {
	var doc OrderCourse
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取OrderCourse
func (e *OrderCourse) Get() (OrderCourse, error) {
	var doc OrderCourse
	table := orm.Eloquent.Table(e.TableName())
	if e.OrderCourseId != 0 {
		table = table.Where("order_course_id = ?", e.OrderCourseId)
	}
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.CourseId != 0 {
		table = table.Where("course_id = ?", e.CourseId)
	}
	if e.CourseName != "" {
		table = table.Where("course_name = ?", e.CourseName)
	}
	if e.CourseCode != "" {
		table = table.Where("course_code = ?", e.CourseCode)
	}
	if e.CourseImg != "" {
		table = table.Where("course_img = ?", e.CourseImg)
	}
	if e.CourseType != "" {
		table = table.Where("course_type = ?", e.CourseType)
	}
	if e.CourseTeach != "" {
		table = table.Where("course_teach = ?", e.CourseTeach)
	}
	if e.CourseCategory != "" {
		table = table.Where("course_category = ?", e.CourseCategory)
	}
	if e.CourseOrganName != "" {
		table = table.Where("course_organ_name = ?", e.CourseOrganName)
	}
	if e.CourseBrandName != "" {
		table = table.Where("course_brand_name = ?", e.CourseBrandName)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrderPay
func (e *OrderCourse) GetList() []OrderCourse {
	var doc []OrderCourse
	table := orm.Eloquent.Select("*").Table(e.TableName())

	if e.OrderId <= 0 {
		e.OrderId = -1
	}
	table = table.Where("order_id = ?", e.OrderId)
	if err := table.Find(&doc).Error; err != nil {
		return nil
	}
	return doc
}

// 获取OrderCourse带分页
func (e *OrderCourse) GetPage(pageSize int, pageIndex int) ([]OrderCourse, int, error) {
	var doc []OrderCourse

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.OrderCourseId != 0 {
		table = table.Where("order_course_id = ?", e.OrderCourseId)
	}
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.CourseId != 0 {
		table = table.Where("course_id = ?", e.CourseId)
	}
	if e.CourseName != "" {
		table = table.Where("course_name = ?", e.CourseName)
	}
	if e.CourseCode != "" {
		table = table.Where("course_code = ?", e.CourseCode)
	}
	if e.CourseImg != "" {
		table = table.Where("course_img = ?", e.CourseImg)
	}
	if e.CourseType != "" {
		table = table.Where("course_type = ?", e.CourseType)
	}
	if e.CourseTeach != "" {
		table = table.Where("course_teach = ?", e.CourseTeach)
	}
	if e.CourseCategory != "" {
		table = table.Where("course_category = ?", e.CourseCategory)
	}
	if e.CourseOrganName != "" {
		table = table.Where("course_organ_name = ?", e.CourseOrganName)
	}
	if e.CourseBrandName != "" {
		table = table.Where("course_brand_name = ?", e.CourseBrandName)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrderCourse
func (e *OrderCourse) Update(id int) (update OrderCourse, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_course_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrderCourse
func (e *OrderCourse) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_course_id = ?", id).Delete(&OrderCourse{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrderCourse) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_course_id in (?)", id).Delete(&OrderCourse{}).Error; err != nil {
		return
	}
	Result = true
	return
}
